package com.blog.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.blog.BO.PlateBO;
import com.blog.dao.AllPlateDao;
import com.blog.dao.BaseDao;
import com.blog.entity.Plate;

public class AllPlateDaoImpl implements AllPlateDao {

	@Override
	public List<Plate> selectPlate(PlateBO plateBO, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		List<Plate> plateList = new ArrayList<Plate>();
		List<Object>  params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("SELECT * FROM allplate where 1 = 1");
		if(plateBO != null) {
			//查询条件
			if(plateBO.getPlateId() != 0) {
				sql.append(" AND plateId = ?");
				params.add(plateBO.getPlateId());
			}//板块id
			if(plateBO.getPlateName()!=null && !"".equals(plateBO.getPlateName())) {
				sql.append(" AND plateName LIKE concat('%',?,'%')");
				params.add(plateBO.getPlateName());
			}//板块名称模糊查询
			if(plateBO.getPlateIconImg()!=null && !"".equals(plateBO.getPlateIconImg())) {
				sql.append(" AND plateIconImg LIKE concat('%',?,'%')");
				params.add(plateBO.getPlateIconImg());
			}//板块图标相关信息模糊查询
			if(plateBO.getPlateState() != 0) {
				sql.append(" AND plateState = ?");
				params.add(plateBO.getPlateState());
			}//板块状态
			if(plateBO.getRegDate()!=null) {
				sql.append(" AND regDate = ?");
				params.add(plateBO.getRegDate());
			}//板块注册的时间
			if(plateBO.getRegStartDate()!=null) {
				sql.append(" AND regDate >= ?");
				params.add(plateBO.getRegStartDate());
			}//板块从什么时间开始，包含输入的时间
			if(plateBO.getRegEndDate()!=null) {
				sql.append(" AND regDate <= ?");
				params.add(plateBO.getRegEndDate());
			}//板块从什么时候结束，包含输入的时间
		}
		sql.append(" LIMIT ?,?");
		Plate selectPlate = null;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql.toString());
			for(int i =0;i<params.size();i++) {
				ps.setObject(i+1, params.get(i));
			}
			ps.setInt(params.size()+1, (pageNum-1)*pageSize);
			ps.setInt(params.size()+2, pageSize);
			res = ps.executeQuery();
			while(res.next()) {
				selectPlate = new Plate();
				selectPlate.setPlateId(res.getInt("plateId"));
				selectPlate.setPlateName(res.getString("plateName"));
				selectPlate.setPlateIconImg(res.getString("plateIconImg"));
				selectPlate.setPlateState(res.getInt("plateState"));
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date dt = null;
				try {
					dt = format.parse(format.format(res.getTimestamp("regDate")));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					System.out.println("转换成Date的String不是规范值");
					e.printStackTrace();
				}
				selectPlate.setRegDate(dt);
				plateList.add(selectPlate);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(res, ps, con);
		}
		return plateList;
	}

	@Override
	public Plate selectPlateByPlateName(String plateName) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		String sql = "SELECT * FROM allplate where plateName = ?";
		Plate selectPlate = null;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1,plateName);
			res = ps.executeQuery();
			if(res.next()) {
				selectPlate = new Plate();
				selectPlate.setPlateId(res.getInt("plateId"));
				selectPlate.setPlateName(res.getString("plateName"));
				selectPlate.setPlateIconImg(res.getString("plateIconImg"));
				selectPlate.setPlateState(res.getInt("plateState"));
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date dt = null;
				try {
					dt = format.parse(format.format(res.getTimestamp("regDate")));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					System.out.println("转换成Date的String不是规范值");
					e.printStackTrace();
				}
				selectPlate.setRegDate(dt);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(res, ps, con);
		}
		return selectPlate;
	}

	@Override
	public int selectPlateNum() {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		String sql = "SELECT COUNT(1) AS plateNum FROM allplate;";
		int plateNum = 0;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			res = ps.executeQuery();
			if(res.next()) {
				plateNum = res.getInt("plateNum");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(res, ps, con);
		}
		return plateNum;
	}

	@Override
	public int insertPlate(Plate plate) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "INSERT INTO allplate(plateName,plateIconImg,plateState,regDate) VALUES (?,?,?,?);";
		int resultNum = 0;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1,plate.getPlateName());
			ps.setString(2,plate.getPlateIconImg());
			ps.setInt(3,plate.getPlateState());
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dt = format.format(plate.getRegDate().getTime());
			ps.setString(4,dt);
			resultNum = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(null, ps, con);
		}
		return resultNum;
	}

	@Override
	public int deletePlateByPlateId(int plateId) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "DELETE FROM allplate WHERE plateId = ?;";
		int resultNum = 0;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1,plateId);
			resultNum = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(null, ps, con);
		}
		return resultNum;
	}

	@Override
	public int updatePlate(Plate plate) {
		// TODO Auto-generated method stub
		List<Object>  params = new ArrayList<Object>();
		int resultNum = 0;
		if(plate.getPlateId() != 0) {
			Connection con = null;
			PreparedStatement ps = null;
			StringBuffer sql = new StringBuffer("UPDATE allplate SET plateId = ?");
			params.add(plate.getPlateId());
			if(plate.getPlateName()!=null && !"".equals(plate.getPlateName())) {
				sql.append(",plateName = ?");
				params.add(plate.getPlateName());
			}//板块名
			if(plate.getPlateIconImg()!=null && !"".equals(plate.getPlateIconImg())) {
				sql.append(",plateIconImg = ?");
				params.add(plate.getPlateIconImg());
			}//板块图标地址
			if(plate.getPlateState() != 0) {
				sql.append(",plateState = ?");
				params.add(plate.getPlateState());
			}//管理员等级
			sql.append(",regDate = ?");
			sql.append(" WHERE plateId = ?");
			try {
				con = BaseDao.getCon();
				ps = con.prepareStatement(sql.toString());
				for(int i =0;i<params.size();i++) {
					ps.setObject(i+1, params.get(i));
				}
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String dt = format.format(new Date().getTime());
				ps.setString(params.size()+1,dt);
				ps.setInt(params.size()+2,plate.getPlateId());
				resultNum = ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				BaseDao.closeAll(null, ps, con);
			}
		}
		return resultNum;
	}

}
